package com.atguigu.guli.service.ucenter.service.impl;

import com.atguigu.commonutils.*;
import com.atguigu.guli.service.base.Exception.GuliException;
import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.vo.LoginVo;
import com.atguigu.guli.service.ucenter.entity.vo.RegisterVo;
import com.atguigu.guli.service.ucenter.mapper.MemberMapper;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author wangyang
 * @since 2021-06-13
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String codeInRedis =(String) redisTemplate.opsForValue().get(mobile);

        //参数统一检验
        if (StringUtils.isEmpty(mobile)|| !FormUtils.isMobile(mobile)
             || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)
             || StringUtils.isEmpty(password)
        ) {
            throw new GuliException(ResultCodeEnum.CODE_ERROR);
        }

        if (!code.equals(codeInRedis) ) {
            throw new GuliException(ResultCodeEnum.CODE_ERROR);
        }

        //查看该手机是否被注册
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new GuliException(ResultCodeEnum.REGISTER_MOBLE_ERROR);
        }

        //注册存入数据库
        Member member = new Member();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password)); //MD5对密码加密
        member.setDisabled(false); //禁用状态
        member.setAvatar("https://guli-school1997.oss-cn-beijing.aliyuncs.com/avatar/2021/06/10/30951c07-ac39-464a-9c57-95c4d453cda4.jpg");
        baseMapper.insert(member);


    }

    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        if (StringUtils.isEmpty(mobile)
            || !FormUtils.isMobile(mobile)
                ||StringUtils.isEmpty(password)
        ) {
            throw new GuliException(ResultCodeEnum.LOGIN_ERROR);
        }

        //校验手机号
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Member member = baseMapper.selectOne(queryWrapper);
        if(member == null){
            throw new GuliException(ResultCodeEnum.LOGIN_PHONE_ERROR);
        }

        //校验密码 MD5加密的方式一样，所以可以加密之后进行比较
        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw new GuliException(ResultCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //判断是都被禁用
        if(member.getDisabled()) {
            throw new GuliException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }

        JwtInfo jwtInfo = new JwtInfo();
        BeanUtils.copyProperties(member,jwtInfo);
        String jwtToken = JwtUtils.getJwtToken(jwtInfo, 1800);
        System.out.println("jwtToken:"+jwtToken);
        return jwtToken;
    }

    @Override
    public MemberDto getMemberDtoByMemberId(String memberId) {
        Member member = baseMapper.selectById(memberId);
        MemberDto memberDto = new MemberDto();
        memberDto.setId(member.getId());
        memberDto.setMobile(member.getMobile());
        memberDto.setNickname(member.getNickname());
        return memberDto;
    }
}
